package com.ygp.dtp.core.convert;

import com.ygp.dtp.common.dto.DynamicThreadPoolMainProperty;
import com.ygp.dtp.core.thread.DynamicThreadPoolExecutor;

import java.util.concurrent.TimeUnit;

/**
 * @description: 转换器
 * @motto: 代码源于生活，高于生活艺术
 * @author: zhouhengzhe
 * @date: 2022/8/4 17:46
 * @since 1.0
 **/
public class ExecutorConverter {
    /**
     * 对象转换DynamicThreadPoolExecutor->DynamicThreadPoolMainProperty
     * @param dynamicThreadPoolExecutor 动态数据源
     * @return
     */
    public static DynamicThreadPoolMainProperty convert(DynamicThreadPoolExecutor dynamicThreadPoolExecutor) {
        return DynamicThreadPoolMainProperty
                .builder()
                .dtpName(dynamicThreadPoolExecutor.getThreadPoolName())
                .corePoolSize(dynamicThreadPoolExecutor.getCorePoolSize())
                .maxPoolSize(dynamicThreadPoolExecutor.getMaximumPoolSize())
                .keepAliveTime(dynamicThreadPoolExecutor.getKeepAliveTime(TimeUnit.SECONDS))
                .queueType(dynamicThreadPoolExecutor.getQueueName())
                .queueCapacity(dynamicThreadPoolExecutor.getQueueCapacity())
                .rejectType(dynamicThreadPoolExecutor.getRejectHandlerName())
                .allowCoreThreadTimeOut(dynamicThreadPoolExecutor.allowsCoreThreadTimeOut())
                .build();
    }
}
